<?php


namespace app\api\controller\v1;
use app\api\model\ParkModel;
use app\api\model\UserRadingOrderModel;
use app\common\controller\UserBaseController;
use common;

class ParkController extends UserBaseController
{

    function infoAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $info = (new ParkModel())->info($this->city_id, $this->argv['id']);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function listAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'state' => 'optint',
            'start_time' => 'optstring',
            'end_time' => 'optstring',
            'sk' => 'optstring',
            'pno' => 'int',
            'psize' => 'int',
            'flush' => ['optint', 0],
            "city_id" => 'optstring',
            'visitable' => 'optint',
        );

        $this->check_params();
        $this->check_token();

        $city_id = $this->city_id;
        if (empty($city_id) && !empty($this->argv['city_id'])) {
            $city_id = $this->argv['city'];
        }
        $list = (new ParkModel())->get_list($city_id, $this->argv, $this->argv['pos'], $this->argv['limit']);
        $this->resp['list'] = $list;

        if (empty($list)) {
            $this->resp['total'] = 0;
        } else {
            $this->resp['total'] = hisql::query_count();
        }

        return $this->ret_resp();
    }

    function addAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            // 'city_id' => 'string',
            'name' => 'string',
            'remark' => 'string',
            'radius' => 'optint',
            'state' => 'int',
            'type' => 'string',
            'capacity' => 'int',
            'grade' => 'int',
            'imgs' => ['optstringarray', ["xxx"]],
            'polygon_type' => ['optint', PARK_POLYGON_TYPE_CIRCLE],
            'coordinate' => 'optarray',
            'coordinates' => 'optpolygon',
            'visitable' => ['optint', 3],
            'markers' => ['optarray', []],
            'coordinates_mask' => 'optarray',
        );

        $this->check_params();
        $this->check_token();

        $info = [
            'city_id' => $this->city_id,
            'name' => $this->argv['name'],
            'remark' => $this->argv['remark'],
            //'coordinate' => $this->argv['coordinate'],
            // 'radius' => $this->argv['radius'],
            'state' => $this->argv['state'],
            'capacity' => $this->argv['capacity'],
            'type' => $this->argv['type'],
            'grade' => $this->argv['grade'],
            'imgs' => $this->argv['imgs'],
            'polygon_type' => $this->argv['polygon_type'],
            'visitable' => $this->argv['visitable'],
            'markers' => $this->argv['markers'],
        ];

        if ($info['polygon_type'] == PARK_POLYGON_TYPE_CIRCLE) {
            if (empty($this->argv['coordinate'])) {
                $this->throw_myexception(E_PARAMS_ERROR, "empty coordinates！");
            }
            $info['coordinate'] = $this->argv['coordinate'];
            $info['coordinates'] = common::circle_to_polygon($this->argv['coordinate'], $this->argv['radius']);
            $info['radius'] = $this->argv['radius'];
        } else {
            if (empty($this->argv['coordinates'])) {
                $this->throw_myexception(E_PARAMS_ERROR, "empty coordinates！");
            }
            $info['coordinates'] = $this->argv['coordinates'];
            $info['coordinate'] = common::polygon_center($this->argv['coordinates']);
            $info['radius'] = (new \geovector())->point_polygon_short_distance($info['coordinate'], $info['coordinates']);
        }


        if (!empty($this->argv['coordinates_mask'])) {
            $info['coordinates_mask'] = $this->argv['coordinates_mask'];
        }

        /*
          $park = (new ParkModel())->is_in($this->city_id, $this->argv['coordinate']);
          if (!empty($park)) {
          throw_myexception(ERROR_OBJ_EXISTS, "于相邻站点距离太近！");
          }
         */

        $infox = (new ParkModel())->add($info);
        $this->resp['info'] = $infox;

        return $this->ret_resp();
    }

    function updateAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
            'name' => 'optstring',
            'remark' => 'optstring',
            'state' => 'optint',
            'type' => 'optstring',
            'capacity' => 'optint',
            'radius' => 'optint',
            'coordinate' => 'optarray',
            'coordinates' => 'optpolygon',
            'grade' => 'optint',
            'polygon_type' => 'optint',
            'visitable' => 'optint',
            'markers' => 'optarray',
            'coordinates_mask' => 'optarray',
        );


        $this->check_params();
        $this->check_token();

        $info = (new ParkModel())->info($this->city_id, $this->argv['id']);
        if (empty($info)) {
            $this->throw_myexception(X_ERROR, "查询仓库信息失败！");
        }

        $data = [];
        foreach ($this->argv as $k => $v) {
            if (array_key_exists($k, $info) && $v !== null) {
                $data[$k] = $v;
            }
        }

        if ($info['polygon_type'] == PARK_POLYGON_TYPE_CIRCLE) {
            if (isset($data['coordinate']) || isset($data['radius'])) {
                $tmp = array_merge($info, $data);
                $data['coordinates'] = common::circle_to_polygon($tmp['coordinate'], $tmp['radius']);
            }
        } else if ($info['polygon_type'] == PARK_POLYGON_TYPE_POLYGON) {
            if (isset($data['coordinates'])) {
                $data['coordinate'] = common::polygon_center($data['coordinates']);
                $data['radius'] = (new geovector())->point_polygon_short_distance($data['coordinate'], $data['coordinates']);
            }
        }
        $info = (new ParkModel())->update($this->city_id, $this->argv['id'], $data);

        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function is_inAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'coordinate' => 'array',
        );
        $this->check_params();
        //  $this->check_token();

        $result = (new ParkModel())->is_in($this->city_id, $this->argv['coordinate']);
        $this->resp['list'] = $result;

        return $this->ret_resp();
    }

    function delAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $info = (new ParkModel())->info($this->city_id, $this->argv['id']);
        if (empty($info)) {
            $this->throw_myexception(X_ERROR, "查询仓库信息失败！");
        }

        (new ParkModel())->delete_pa($this->city_id, $this->argv['id']);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function ranklistAction()
    {
        $this->paramlist = [
            'start_date' => 'string',
            'end_date' => 'string',
        ];

        $this->check_params();
        $this->check_token();

        $l = (new UserRadingOrderModel())->start_park_stats($this->city_id, $this->argv['start_date'], $this->argv['end_date']);
        $this->resp['list'] = $l;

        return $this->ret_resp();
    }

}
